<?php
class Data {
    var $con;
    public function __construct() {
    }
    public function connect($dbname,$iswrite=1) {
        global $DATA_CONFIG;
        $db_info = $DATA_CONFIG[$dbname];
        if($iswrite) {
            if(!$this->con = @mysql_connect($db_info['host'],$db_info['user'],$db_info['passwd'])) {
                return false;
            }
            $db_name = $db_info['dbname'];
        } else {
            if(!$this->con = @mysql_connect($db_info['host_r'],$db_info['user_r'],$db_info['passwd_r'])) {
                return false;
            }
            $db_name = $db_info['dbname_r'];
        }
        return mysql_select_db($db_name,$this->con)? $this : false;
    }
    /**
     * 执行查询
     * @param  $sql     SQL语句
     * @return 
     */
    public function query($sql,$charset=0) {
        if($charset) mysql_query("set names '".CHARTSET."'");
        $return = mysql_query($sql);
        return $return;
    }
    /**
     * 获得查询结果 只返回字符key
     * @param $column
     * @return
     */
    public function fetch($return,$column='') {
        if(!$return) {
            return false;
        }
        $reulst = array();
        if(empty($column)) {
            while($row = mysql_fetch_assoc($return)) {
                $reulst[] = $row;
            }
        } else {
            while($row = mysql_fetch_assoc($return)) {
                $col = $row[$column];
                $reulst[$col] = $row;
            }
        }
        return $reulst;
    }
    /**
     * 返回数字key和字符key数组
     * @param $type
     * @return 
     */
    public function fetchArray($type=MYSQL_BOTH) {
        if(!$return) {
            return false;
        }
        $result = array();
        while($row = mysql_fetch_array($return,$type)) {
            $reulst[] = $row;
        }
        return $result;
    }
    /**
     * 返回一行
     * @return
     */
    public function fetchRow($return) {
        if(!$return) {
            return false;
        }
        return mysql_fetch_row($return);
    }
    public function insertId() {
        return mysql_insert_id();
    }
    public function allRows() {
        return mysql_affected_rows();
    }

}
?>